/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vaadin.demo.dashboard;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.quick.bean.NewsfeedBean;
import com.quick.global.GlobalConstants;
import com.quick.newsfeed.NewsfeedWraper;
import com.quick.utilities.LoadEarlierBtnWraper;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.vaadin.event.LayoutEvents;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.*;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author suyogn
 */
public class HomeView extends VerticalLayout implements View, LayoutEvents.LayoutClickListener{
    
    
    private HorizontalLayout row ;
    
    @Override
    public void enter(ViewChangeEvent event) {
        setSizeFull();
        addStyleName("schedule");
        setMargin(false);
        setSizeFull();
    }
    
    
    public HomeView()
    {
        
        /* HorizontalLayout topDummy = new HorizontalLayout();
        topDummy.setWidth("100%");
        topDummy.setSpacing(true);
        topDummy.setMargin(true);
        topDummy.addStyleName("whiteBg");
        
        Label dummyWhiteLabel = new Label(GlobalConstants.emptyString);
        topDummy.addComponent(dummyWhiteLabel);
        topDummy.setComponentAlignment(dummyWhiteLabel, Alignment.MIDDLE_LEFT);
        topDummy.setExpandRatio(dummyWhiteLabel, 1);
        
        addComponent(topDummy); */
        
        /*  final Label title = new Label("Newsfeed");
        title.setSizeUndefined();
        title.addStyleName("selectedMenuBanner");
        
        HorizontalLayout top = new HorizontalLayout();
        top.setWidth("100%");
        top.setSpacing(true);
        top.setMargin(true);
        top.addStyleName(GlobalConstants.toolbar_style);
        top.addStyleName("selectedMenuBanner");
        top.addStyleName("lightGrayFourSideBorder");
        
        addComponent(top);
        top.addComponent(title);
        top.setComponentAlignment(title, Alignment.MIDDLE_LEFT);
        top.setExpandRatio(title, 1); */

      
        row = new HorizontalLayout();
        row.setHeight("100%");
        row.setWidth("100%");
        row.setMargin(new MarginInfo(true, true, false, true));
        row.setSpacing(true);
        
        Panel p = new Panel();
        p.setSizeFull();
        p.setContent(row);
        //p.addStyleName("fourSideWhiteBorder");
        
        addComponent(p);
        setExpandRatio(p, 1.5f);
        setComponentAlignment(p, Alignment.MIDDLE_CENTER);
        
        
        buildBody();
        
//        row.addComponent(aboutImage);        
//        row.setExpandRatio(aboutImage, 1.5f);
//        row.setComponentAlignment(aboutImage, Alignment.TOP_RIGHT);

    }

    
    @Override
    public void layoutClick(LayoutEvents.LayoutClickEvent event) 
    {
        Component c = event.getComponent();
        
    }
    
    private List <NewsfeedBean> newsfeedDetailsList;
    private List<List> wrapperList = new ArrayList<List>();
    private Table newsfeedTable;
    private LoadEarlierBtnWraper loadMoreWraper = new LoadEarlierBtnWraper(this);
    
    private List<NewsfeedBean> getNewsfeedList() {
        try {
            Client client = Client.create();
            WebResource webResource = client.resource(GlobalConstants.getProperty(GlobalConstants.GET_All_NEWSFEED));
            //String input = "{\"userName\":\"raj\",\"password\":\"FadeToBlack\"}";
            JSONObject inputJson = new JSONObject();
             try
             {           
                 if (newsfeedTable == null) {
                     inputJson.put("fetchResultsFrom", 0);
                 } else {
                     inputJson.put("fetchResultsFrom", (newsfeedTable.size() - 1));
                 }
//                inputJson.put("username",userprofile.getUsername());
             }catch(Exception ex){
                 ex.printStackTrace();
             }
            
            ClientResponse response = webResource.type(GlobalConstants.application_json).post(ClientResponse.class, inputJson);
            
            JSONObject outNObject = null;
            String output = response.getEntity(String.class);
            outNObject = new JSONObject(output);

            java.lang.reflect.Type listType = new TypeToken<ArrayList<NewsfeedBean>>() {
            }.getType();
    
            Gson gson=  new GsonBuilder().setDateFormat(GlobalConstants.gsonTimeFormat).create();
            
            newsfeedDetailsList=gson.fromJson(outNObject.getString(GlobalConstants.newsfeedList), listType);
            if(newsfeedDetailsList.size()>0)
            {
                wrapperList.add(newsfeedDetailsList);
            }
            
            
        } catch (JSONException ex) 
        {
            ex.printStackTrace();
          //  L.getLogger(AddStudent.class.getName()).log(Level.SEVERE, null, ex);
        }
        return newsfeedDetailsList;
    }

    private void buildBody() 
    {
        getNewsfeedList();
        
        newsfeedTable  = new Table();
          
        newsfeedTable.addContainerProperty(GlobalConstants.emptyString, VerticalLayout.class, null);
        newsfeedTable.addStyleName("plain");
        newsfeedTable.addStyleName("borderless");
        //newsfeedTable.setHeight("100%");
        newsfeedTable.setWidth("100%");
        newsfeedTable.setSortEnabled(false);
        newsfeedTable.setColumnHeaderMode(Table.ColumnHeaderMode.HIDDEN);
        
        addNewsfeedInToTable();
        
        
        row.addComponent(newsfeedTable);
        row.setExpandRatio(newsfeedTable, 1.5f);

    }
    
    private void addNewsfeedInToTable() 
    {
        for(List<NewsfeedBean> eventDetailsList:wrapperList)
        {
            for(NewsfeedBean eventDetails:eventDetailsList)
            {
                newsfeedTable.addItem(new Object[]{new NewsfeedWraper(eventDetails,this) },newsfeedTable.size()+1);
            }            
        }
        newsfeedTable.addItem(new Object[]{loadMoreWraper},newsfeedTable.size()+1);
    }
}
